/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.service;

import br.com.foxline.clinicplus.entity.Category;
import br.com.foxline.clinicplus.entity.Item;
import br.com.foxline.clinicplus.entity.TemplateAppraisal;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author roniere
 */
@Stateless
public class ItemService extends GenericService<Item> {

    public ItemService() {
        super(Item.class);
    }

    public List<Item> getByOrdemAsc(Category category) {
        String sql = "SELECT i FROM Category c JOIN c.items i  WHERE";

        if (category != null) {
            sql += " c.id = :id ";
        }

        sql += " order by i.ordem asc ";

        Query q = getEntityManager().createQuery(sql);

        if (category != null) {
            q.setParameter("id", category.getId());
        }

        return (List<Item>) q.getResultList();
    }

    public List<Item> pesquisarFrase(String frase) {
        String sql = "SELECT i FROM Item i WHERE";
        sql += " lower(i.descricao) like lower(:frase) ";

        Query q = getEntityManager().createQuery(sql);

        q.setParameter("frase", "%"+frase+"%");

        return (List<Item>) q.getResultList();
    }
}
